//
//  ICBaseStaticTableViewController.m
//  Youyouhui
//
//  Created by 陈 颜俊 on 14/11/12.
//  Copyright (c) 2014年 UULian. All rights reserved.
//

#import "ICBaseTableViewController.h"
@interface ICBaseTableViewController () <MBProgressHUDDelegate, UIAlertViewDelegate>
@end

@implementation ICBaseTableViewController
@synthesize  hud = _hud;
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)dealloc
{
    self.hud.delegate = nil;
}

- (void)showHud
{
    self.hud = [self hudForView:self.view];
    
    if (self.hud.alpha == 0) {
        self.hud.alpha = 1;
    }
}

- (MBProgressHUD *)hudForView:(UIView *)view
{
    self.hud = [MBProgressHUD HUDForView:view];
    if (!self.hud) {
        self.hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        self.hud.delegate = self;
    }
    
    return self.hud;
}

- (void)hideHud
{
    [self.hud hide:YES];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

- (void)hideHudAfterDelay:(NSTimeInterval)delay
{
    [self.hud hide:YES afterDelay:delay];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

- (void)hudWasHidden:(MBProgressHUD *)hud
{
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = nil;
    hud.detailsLabelText = nil;
}

- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message handle:(AlertShowHandle)handle
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (handle) {
            handle();
        }
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message confirmHandle:(AlertShowHandle)confirmHandle cancleHandle:(AlertShowHandle)cancleHandle
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (cancleHandle) {
            cancleHandle();
        }
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (confirmHandle) {
            confirmHandle();
        }
    }]];
    [self presentViewController:alertController animated:YES completion:nil];
}

- (void)showToastWithTitle:(NSString *)title
{
    self.hud = [self hudForView:self.view];
    self.hud.mode = MBProgressHUDModeText;
    self.hud.labelText = title;
    [self hideHudAfterDelay:1.0f];
}

@end
